package com.smart.opcda.controller;


import cn.hutool.core.util.StrUtil;
import com.smart.common.vo.BaseResp;
import com.smart.opcda.dto.*;
import com.smart.opcda.entity.RegisterGroupResponse;
import com.smart.opcda.entity.TopicEntity;
import com.smart.opcda.entity.UnRegisterGroupResponse;
import com.smart.opcda.service.OpcDAService;
import com.smart.opcda.service.OpcMemeryManager;
import com.smart.opcda.service.OpcDAManager;
import com.smart.opcda.topic.ServerManager;
import com.smart.opcda.topic.TopicManager;
import com.smart.opcda.util.VariantUtil;
import javafish.clients.opc.JEasyOpc;
import javafish.clients.opc.component.OpcGroup;
import javafish.clients.opc.component.OpcItem;
import javafish.clients.opc.variant.Variant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * opc操作
 */

@RestController
@RequestMapping("/opc")
@Slf4j
public class OpcDAController {

    @Autowired
    private OpcDAManager opcDAManager;

    @Autowired
    private TopicManager topicManager;

    @Autowired
    private ServerManager serverManager;

    @Autowired
    private OpcMemeryManager opcMemeryManager;

    @Autowired
    private OpcDAService opcDAService;


    @RequestMapping("/subGroup")
    public BaseResp subGroup(@RequestBody List<GroupSubRequest> groupSubRequests) {
        try {
            if (groupSubRequests == null || groupSubRequests.size() == 0) {
                return BaseResp.fail("参数错误");
            }
            log.info(groupSubRequests.toString());
            RegisterGroupResponse registerGroupResponse = opcDAService.registerGroup(groupSubRequests, true);
            if (registerGroupResponse.getSuccessedGroups().size() > 0) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
            Set<String> groupSet = new HashSet<>();
            if (registerGroupResponse.getSuccessedGroups().size() > 0) {
                for (GroupSubRequest groupSubRequest : registerGroupResponse.getSuccessedGroups()) {
                    String topicName = groupSubRequest.getTopic();
                    TopicEntity topicEntity = topicManager.getTopicMap().get(topicName);
                    if (topicEntity != null) {
                        JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(topicEntity.getServerId());
                        if (jEasyOpc != null) {
                            OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                            for (OpcGroup opcGroup : opcGroups) {
                                if (groupSubRequest.getGroupId().equals(opcGroup.getGroupName())) {
                                    jEasyOpc.asynch20Read(opcGroup);
                                    groupSet.add(opcGroup.getGroupName());
                                }
                            }
                        }
                    }
                }
            }
            if (registerGroupResponse.getSuccessedItems().size() > 0) {
                for (GroupSubItemResponse groupSubItemResponse : registerGroupResponse.getSuccessedItems()) {
                    String topicName = groupSubItemResponse.getTopicName();
                    TopicEntity topicEntity = topicManager.getTopicMap().get(topicName);
                    if (topicEntity != null) {
                        JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(topicEntity.getServerId());
                        if (jEasyOpc != null) {
                            OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                            for (OpcGroup opcGroup : opcGroups) {
                                if (groupSubItemResponse.getGroupId().equals(opcGroup.getGroupName()) && !groupSet.contains(groupSubItemResponse.getGroupId())) {
                                    jEasyOpc.asynch20Unadvise(opcGroup);
                                    Thread.sleep(1000);
                                    jEasyOpc.asynch20Read(opcGroup);
                                    groupSet.add(groupSubItemResponse.getGroupId());
                                }
                            }
                        }
                    }
                }
            }


            return BaseResp.success("订阅成功的Item=" + registerGroupResponse.getSuccessedItems().toString() + ",订阅失败的Item=" + registerGroupResponse.getFaildedGroups().toString() + ",已存在无需重复订阅的Item=" + registerGroupResponse.getRepeatedItems().toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    @RequestMapping("/unSubGroup")
    public BaseResp unSubGroup(@RequestBody List<GroupSubRequest> groupSubRequests) {
        try {
            if (groupSubRequests == null || groupSubRequests.size() == 0) {
                return BaseResp.fail("参数错误");
            }
            log.info(groupSubRequests.toString());
            UnRegisterGroupResponse unRegisterGroupResponse = opcDAService.unRegisterGroup(groupSubRequests);

            if (unRegisterGroupResponse.getSuccessedGroups().size() > 0) {
                for (GroupSubRequest groupSubRequest : unRegisterGroupResponse.getSuccessedGroups()) {
                    opcMemeryManager.getOpcGroupMap().remove(groupSubRequest.getGroupId());
                }
            }
            return BaseResp.success("取消订阅成功的Group=" + unRegisterGroupResponse.getSuccessedGroups().toString() + ",取消订阅失败的Group=" + unRegisterGroupResponse.getFaildedGroups().toString() + ",不存在的Group=" + unRegisterGroupResponse.getNonexistentGroups().toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    @RequestMapping("/getAllServerGroupItem")
    public BaseResp getAllServerGroupItem() {
        try {
            List<Server> servers = new ArrayList<>();
            Map<String, Server> serverMap = serverManager.getServerMap();
            if (serverMap != null && serverMap.size() > 0) {
                for (String server : serverMap.keySet()) {
                    Server server1 = new Server();
                    server1.setServerId(server);
                    server1.setServerName(serverMap.get(server).getServerName());
                    JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(server);
                    if (jEasyOpc != null) {
                        OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                        if (opcGroups.length > 0) {
                            List<Group> groups = new ArrayList<>();
                            for (OpcGroup opcGroup : opcGroups) {
                                Group group = new Group();
                                group.setGroupName(opcGroup.getGroupName());
                                group.setUpdateRate(opcGroup.getUpdateRate());
                                List<OpcItem> list = opcGroup.getItems();
                                if (list != null && list.size() > 0) {
                                    List<Item> items = new ArrayList<>();
                                    for (OpcItem opcItem : list) {
                                        Item item = new Item();
                                        item.setItemName(opcItem.getItemName());
                                        items.add(item);
                                    }
                                    group.setItems(items);
                                }
                                groups.add(group);
                            }
                            server1.setGroups(groups);
                        }
                    }
                    servers.add(server1);
                }
            }
            return BaseResp.success("", servers);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    @RequestMapping("/getOpcMemeryGroup")
    public BaseResp getOpcMemeryGroup() {
        try {
            Map<String, Map<String, ReadDataResponse>> opcGroupMap = opcMemeryManager.getOpcGroupMap();
            return BaseResp.success("查询成功", opcGroupMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    @RequestMapping("/read")
    public BaseResp read(@RequestBody List<GroupSubRequest> groupSubRequests) {
        try {
            List<ReadDataResponse> readDataResponses = new ArrayList<>();
            if (groupSubRequests == null || groupSubRequests.size() == 0) {
                return BaseResp.fail("参数错误");
            }
            log.info(groupSubRequests.toString());
            List<GroupSubRequest> readGroupSubRequests = new ArrayList<>();
            RegisterGroupResponse registerGroupResponse = opcDAService.registerGroup(groupSubRequests, false);
            if (registerGroupResponse.getSuccessedGroups().size() > 0) {
                readGroupSubRequests.addAll(registerGroupResponse.getSuccessedGroups());
            }
            if (registerGroupResponse.getSuccessedGroups().size() > 0 || registerGroupResponse.getSuccessedItems().size() > 0) {
                Thread.sleep(1000);
            }

            if (registerGroupResponse.getExistentGroups().size() > 0) {
                readGroupSubRequests.addAll(registerGroupResponse.getExistentGroups());
            }

            List<GroupSubItemResponse> readGroupSubItems = new ArrayList<>();
            if (registerGroupResponse.getSuccessedItems().size() > 0) {
                readGroupSubItems.addAll(registerGroupResponse.getSuccessedItems());
            }
            if (registerGroupResponse.getRepeatedItems().size() > 0) {
                readGroupSubItems.addAll(registerGroupResponse.getRepeatedItems());
            }
            if (readGroupSubRequests.size() > 0) {
                for (GroupSubRequest groupSubRequest : readGroupSubRequests) {
                    String topicName = groupSubRequest.getTopic();
                    TopicEntity topicEntity = topicManager.getTopicMap().get(topicName);
                    String accessPath = topicEntity.getAccessPath();
                    if (StrUtil.isNotBlank(accessPath)) {
                        accessPath += ".";
                    }
                    JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(topicEntity.getServerId());
                    OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                    for (OpcGroup opcGroup : opcGroups) {
                        if (groupSubRequest.getGroupId().equals(opcGroup.getGroupName())) {
                            try {
                                OpcGroup group = jEasyOpc.synchReadGroup(opcGroup);
                                List<OpcItem> opcItems = group.getItems();
                                if (opcItems != null && opcItems.size() > 0) {
                                    for (OpcItem opcItem : opcItems) {
                                        String itemName = opcItem.getItemName();
                                        GregorianCalendar gregorianCalendar = opcItem.getTimeStamp();
                                        Date timeStamp;
                                        if (gregorianCalendar != null) {
                                            timeStamp = gregorianCalendar.getTime();
                                        } else {
                                            timeStamp = new Date();
                                        }
                                        boolean isQuality = opcItem.isQuality();
                                        Object value = VariantUtil.convertOpcValueToJava(opcItem);
                                        String item = itemName.replaceAll(accessPath, "");
                                        String dataType = Variant.getVariantName(opcItem.getDataType());
                                        ReadDataResponse readDataResponse = new ReadDataResponse();
                                        readDataResponse.setTopicName(topicName);
                                        readDataResponse.setItemId(itemName);
                                        readDataResponse.setItem(item);
                                        readDataResponse.setTimeStamp(timeStamp);
                                        readDataResponse.setValue(value);
                                        readDataResponse.setDataType(dataType);
                                        readDataResponse.setItemQuality(isQuality);
                                        if (readGroupSubItems.size() > 0) {
                                            for (GroupSubItemResponse groupSubItem : readGroupSubItems) {
                                                if (groupSubItem.getItemId().equals(itemName) && groupSubItem.getTopicName().equals(topicName)) {
                                                    readDataResponses.add(readDataResponse);
                                                }
                                            }
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                List<GroupSubItem> groupSubItems = groupSubRequest.getItems();
                                if (groupSubItems != null && groupSubItems.size() > 0) {
                                    for (GroupSubItem groupSubItem : groupSubItems) {
                                        ReadDataResponse readDataResponse = new ReadDataResponse();
                                        readDataResponse.setTopicName(groupSubRequest.getTopic());
                                        readDataResponse.setItemId(groupSubItem.getItemId());
                                        readDataResponse.setItem(groupSubItem.getItemId());
                                        readDataResponse.setTimeStamp(new Date());
                                        readDataResponse.setItemQuality(false);
                                        readDataResponse.setValue("err");
                                        readDataResponses.add(readDataResponse);
                                    }
                                }
                                log.error(e.getMessage(), e);
                            }
                        }
                    }
                }
            }

            if (registerGroupResponse.getFaildedGroups().size() > 0) {
                for (GroupSubRequest groupSubRequest : registerGroupResponse.getFaildedGroups()) {
                    List<GroupSubItem> groupSubItems = groupSubRequest.getItems();
                    if (groupSubItems != null && groupSubItems.size() > 0) {
                        for (GroupSubItem groupSubItem : groupSubItems) {
                            ReadDataResponse readDataResponse = new ReadDataResponse();
                            readDataResponse.setTopicName(groupSubRequest.getTopic());
                            readDataResponse.setItemId(groupSubItem.getItemId());
                            readDataResponse.setItem(groupSubItem.getItemId());
                            readDataResponse.setTimeStamp(new Date());
                            readDataResponse.setItemQuality(false);
                            readDataResponse.setValue("err");
                            readDataResponses.add(readDataResponse);
                        }
                    }
                }
            }

            if (registerGroupResponse.getFaildedItems().size() > 0) {
                for (GroupSubItemResponse groupSubItemResponse : registerGroupResponse.getFaildedItems()) {
                    ReadDataResponse readDataResponse = new ReadDataResponse();
                    readDataResponse.setTopicName(groupSubItemResponse.getTopicName());
                    readDataResponse.setItemId(groupSubItemResponse.getItemId());
                    readDataResponse.setItem(groupSubItemResponse.getItem());
                    readDataResponse.setTimeStamp(new Date());
                    readDataResponse.setItemQuality(false);
                    readDataResponse.setValue("err");
                    readDataResponses.add(readDataResponse);
                }
            }

            return BaseResp.success("查询成功", readDataResponses);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    @RequestMapping("/write")
    public BaseResp write(@RequestBody List<GroupSubRequest> groupSubRequests) {
        try {
            List<ReadDataResponse> readDataResponses = new ArrayList<>();
            if (groupSubRequests == null || groupSubRequests.size() == 0) {
                return BaseResp.fail("参数错误");
            }
            log.info(groupSubRequests.toString());
            RegisterGroupResponse registerGroupResponse = opcDAService.registerGroup(groupSubRequests, false);
            List<GroupSubItemResponse> readGroupSubItems = new ArrayList<>();
            if (registerGroupResponse.getSuccessedItems().size() > 0) {
                readGroupSubItems.addAll(registerGroupResponse.getSuccessedItems());
            }
            if (registerGroupResponse.getRepeatedItems().size() > 0) {
                readGroupSubItems.addAll(registerGroupResponse.getRepeatedItems());
            }
            Map<String, OpcGroup> opcGroupMap = new HashMap<>();
            Map<String, JEasyOpc> jEasyOpcMap = new HashMap<>();
            Map<String, TopicEntity> topicEntityMap = new HashMap<>();
            Set<GroupSubItemResponse> writeSuccessed = new HashSet<>();
            if (readGroupSubItems != null && readGroupSubItems.size() > 0) {
                for (GroupSubItemResponse groupSubItemResponse : readGroupSubItems) {
                    TopicEntity topicEntity = topicManager.getTopicMap().get(groupSubItemResponse.getTopicName());
                    JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(topicEntity.getServerId());
                    OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                    for (OpcGroup opcGroup : opcGroups) {
                        if (opcGroup.getGroupName().equals(groupSubItemResponse.getGroupId())) {
                            List<OpcItem> opcItems = opcGroup.getItems();
                            for (OpcItem opcItem : opcItems) {
                                if (opcItem.getItemName().equals(groupSubItemResponse.getItemId())) {
                                    Variant variant;
                                    if (groupSubItemResponse.getValue() instanceof Byte) {
                                        variant = new Variant(Byte.class.cast(Byte.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof Short) {
                                        variant = new Variant(Short.class.cast(Short.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof Integer) {
                                        variant = new Variant(Integer.class.cast(Integer.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof Float) {
                                        variant = new Variant(Float.class.cast(Float.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof Double) {
                                        variant = new Variant(Double.class.cast(Double.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof Boolean) {
                                        variant = new Variant(Boolean.class.cast(Boolean.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else if (groupSubItemResponse.getValue() instanceof String) {
                                        variant = new Variant(String.class.cast(String.valueOf(groupSubItemResponse.getValue().toString())));
                                    } else {
                                        throw new IllegalArgumentException("Unsupported conversion type");
                                    }
                                    opcItem.setValue(variant);
                                    try {
                                        jEasyOpc.synchWriteItem(opcGroup, opcItem);
                                        writeSuccessed.add(groupSubItemResponse);
                                        opcGroupMap.put(opcGroup.getGroupName(), opcGroup);
                                        jEasyOpcMap.put(opcGroup.getGroupName(), jEasyOpc);
                                        topicEntityMap.put(opcGroup.getGroupName(), topicEntity);
                                    } catch (Exception e) {
                                        log.error(e.getMessage(), e);
                                        ReadDataResponse readDataResponse = new ReadDataResponse();
                                        readDataResponse.setTopicName(groupSubItemResponse.getTopicName());
                                        readDataResponse.setItemId(groupSubItemResponse.getItemId());
                                        readDataResponse.setItem(groupSubItemResponse.getItem());
                                        readDataResponse.setTimeStamp(new Date());
                                        readDataResponse.setItemQuality(false);
                                        readDataResponse.setValue("err");
                                        readDataResponses.add(readDataResponse);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (writeSuccessed.size() > 0) {
                //全系统找到写入tag的所有group
                boolean f = processWriteItemAsyncLogic(writeSuccessed);
                if (!f) {
                    Thread.sleep(1000);
                }
                for (String groupId : opcGroupMap.keySet()) {
                    JEasyOpc jEasyOpc = jEasyOpcMap.get(groupId);
                    OpcGroup opcGroup = opcGroupMap.get(groupId);
                    TopicEntity topicEntity = topicEntityMap.get(groupId);
                    try {
                        OpcGroup group = jEasyOpc.synchReadGroup(opcGroup);
                        List<OpcItem> opcItems = group.getItems();
                        if (opcItems != null && opcItems.size() > 0) {
                            for (OpcItem opcItem : opcItems) {
                                String itemName = opcItem.getItemName();
                                GregorianCalendar gregorianCalendar = opcItem.getTimeStamp();
                                Date timeStamp;
                                if (gregorianCalendar != null) {
                                    timeStamp = gregorianCalendar.getTime();
                                } else {
                                    timeStamp = new Date();
                                }
                                boolean isQuality = opcItem.isQuality();
                                Object value = VariantUtil.convertOpcValueToJava(opcItem);
                                String accessPath = topicEntity.getAccessPath();
                                if (StrUtil.isNotBlank(accessPath)) {
                                    accessPath += ".";
                                }
                                String item = itemName.replaceAll(accessPath, "");
                                String dataType = Variant.getVariantName(opcItem.getDataType());
                                ReadDataResponse readDataResponse = new ReadDataResponse();
                                readDataResponse.setTopicName(topicEntity.getTopicName());
                                readDataResponse.setItemId(itemName);
                                readDataResponse.setItem(item);
                                readDataResponse.setTimeStamp(timeStamp);
                                readDataResponse.setValue(value);
                                readDataResponse.setDataType(dataType);
                                readDataResponse.setItemQuality(isQuality);
                                for (GroupSubItemResponse groupSubItemResponse : writeSuccessed) {
                                    if (groupSubItemResponse.getItemId().equals(itemName) && groupSubItemResponse.getTopicName().equals(topicEntity.getTopicName())) {
                                        readDataResponses.add(readDataResponse);
                                    }
                                }

                            }
                        }
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        List<OpcItem> opcItems = opcGroup.getItems();
                        if (opcItems != null && opcItems.size() > 0) {
                            for (OpcItem opcItem : opcItems) {
                                ReadDataResponse readDataResponse = new ReadDataResponse();
                                readDataResponse.setTopicName(topicEntity.getTopicName());
                                readDataResponse.setItemId(opcItem.getItemName());
                                String accessPath = topicEntity.getAccessPath();
                                if (StrUtil.isNotBlank(accessPath)) {
                                    accessPath += ".";
                                }
                                String item = opcItem.getItemName().replaceAll(accessPath, "");
                                readDataResponse.setItem(item);
                                readDataResponse.setTimeStamp(new Date());
                                readDataResponse.setItemQuality(false);
                                readDataResponse.setValue("err");
                                if (writeSuccessed.contains(opcItem.getItemName())) {
                                    readDataResponses.add(readDataResponse);
                                }
                            }
                        }
                    }
                }
            }

            if (registerGroupResponse.getFaildedGroups().size() > 0) {
                for (GroupSubRequest groupSubRequest : registerGroupResponse.getFaildedGroups()) {
                    List<GroupSubItem> groupSubItems = groupSubRequest.getItems();
                    if (groupSubItems != null && groupSubItems.size() > 0) {
                        for (GroupSubItem groupSubItem : groupSubItems) {
                            ReadDataResponse readDataResponse = new ReadDataResponse();
                            readDataResponse.setTopicName(groupSubRequest.getTopic());
                            readDataResponse.setItemId(groupSubItem.getItemId());
                            readDataResponse.setItem(groupSubItem.getItemId());
                            readDataResponse.setTimeStamp(new Date());
                            readDataResponse.setItemQuality(false);
                            readDataResponse.setValue("err");
                            readDataResponses.add(readDataResponse);
                        }
                    }
                }
            }

            if (registerGroupResponse.getFaildedItems().size() > 0) {
                for (GroupSubItemResponse groupSubItemResponse : registerGroupResponse.getFaildedItems()) {
                    ReadDataResponse readDataResponse = new ReadDataResponse();
                    readDataResponse.setTopicName(groupSubItemResponse.getTopicName());
                    readDataResponse.setItemId(groupSubItemResponse.getItemId());
                    readDataResponse.setItem(groupSubItemResponse.getItem());
                    readDataResponse.setTimeStamp(new Date());
                    readDataResponse.setItemQuality(false);
                    readDataResponse.setValue("err");
                    readDataResponses.add(readDataResponse);
                }
            }

            return BaseResp.success("执行成功", readDataResponses);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return BaseResp.error("系统错误");
    }

    private boolean processWriteItemAsyncLogic(Set<GroupSubItemResponse> set) {
        boolean hasProcess = false;
        Map<String, OpcGroup> opcGroupMap = new HashMap<>();
        for (GroupSubItemResponse groupSubItemResponse : set) {
            String topicName = groupSubItemResponse.getTopicName();
            TopicEntity topicEntity = topicManager.getTopicMap().get(topicName);
            log.info("TopicName=" + topicName);
            if (topicEntity != null) {
                JEasyOpc jEasyOpc = opcDAManager.getOpcInstance(topicEntity.getServerId());
                if (jEasyOpc != null) {
                    OpcGroup[] opcGroups = jEasyOpc.getGroupsAsArray();
                    if (opcGroups.length > 0) {
                        for (OpcGroup opcGroup : opcGroups) {
                            List<OpcItem> opcItems = opcGroup.getItems();
                            if (opcItems.size() > 0) {
                                for (OpcItem opcItem : opcItems) {
                                    if (groupSubItemResponse.getItemId().equals(opcItem.getItemName()) && !opcGroupMap.containsKey(opcGroup.getGroupName())) {
                                        opcGroupMap.put(opcGroup.getGroupName(), opcGroup);
                                    }
                                }
                            }
                        }
                        if (opcGroupMap.size() > 0) {
                            for (String groupId : opcGroupMap.keySet()) {
                                OpcGroup opcGroup = opcGroupMap.get(groupId);
                                if (opcGroup.getAsynchListeners().getListenerCount() > 0) {
                                    try {
                                        jEasyOpc.asynch20Unadvise(opcGroup);
                                        Thread.sleep(1000);
                                        jEasyOpc.asynch20Read(opcGroup);
                                        hasProcess = true;
                                    } catch (Exception e) {
                                        log.error(e.getMessage(), e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return hasProcess;
    }
}
